package com.dkm.orproject.dkmoa.service.serviceimpl.jurisdiction;

import com.dkm.orproject.dkmoa.dao.OaJurisdictionMapper;
import com.dkm.orproject.dkmoa.dao.OaJurisdictionRoleMapper;
import com.dkm.orproject.dkmoa.domain.OaJurisdiction;
import com.dkm.orproject.dkmoa.service.jurisdiction.OaJurisdictionService;
import com.dkm.orproject.utils.utils.IdToobtainUtils;
import com.dkm.orproject.utils.utils.ThreeVo;
import com.dkm.orproject.utils.utils.Tools;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : JurisdictionServiceImpl  //类名
 * @Description : 权限服务接口实现类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-08-17 09:52  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OaJurisdictionServiceImpl implements OaJurisdictionService {


    /**
     * 权限dao
     */
    @Resource
    OaJurisdictionMapper jurisdictionMapper;
    @Resource
    OaJurisdictionRoleMapper jurisdictionRoleMapper;

    /**
     * id生成工具
     */
    @Resource
    IdToobtainUtils idToobtainUtils;
    @Resource
    Tools tools;

    /**
     * 分页查询
     * @param jurisdiction 权限模型 用于条件查询
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 当前页数
     * @param limit 显示行数
     * @return 权限模型集
     */
    @Override
    public List<OaJurisdiction> findAll(OaJurisdiction jurisdiction, String startTime, String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return jurisdictionMapper.findAll(jurisdiction,startTime,endTime);
    }

    /**
     * 总行数
     * @param jurisdiction 权限模型 用于条件查询
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 总行数
     */
    @Override
    public Long rowCount(OaJurisdiction jurisdiction, String startTime, String endTime) {
        return jurisdictionMapper.rowCount(jurisdiction,startTime,endTime);
    }

    /**
     * id查询
     * @param id 权限id
     * @return 权限模型
     */
    @Override
    public OaJurisdiction findById(Long id) {
        return jurisdictionMapper.selectByPrimaryKey(id);
    }

    /**
     * 权限代码查询
     * @param key 权限代码
     * @return 权限模型集
     */
    @Override
    public List<OaJurisdiction> findKeyword(String key) {
        return jurisdictionMapper.findKeyword(key);
    }

    /**
     * 修改
     * @param jurisdiction 权限模型
     * @param personnelName 当前操作人
     * @return 回调函数
     */
    @Override
    public int update(OaJurisdiction jurisdiction, String personnelName) {
        jurisdiction.setUpdatedBy(personnelName);
        jurisdiction.setUpdatedTime(new Date());
        return jurisdictionMapper.updateByPrimaryKeySelective(jurisdiction);
    }

    /**
     * 新增
     * @param jurisdiction 权限模型
     * @param personnelName 当前操作人
     * @return 回调函数
     */
    @Override
    public int add(OaJurisdiction jurisdiction, String personnelName) {
        if(jurisdiction.getJurisdictionSuperior()==null){
            jurisdiction.setJurisdictionSuperior(0L);
        }
        jurisdiction.setPkJurisdictionId(idToobtainUtils.getNumberId());
        jurisdiction.setCreatedBy(personnelName);
        jurisdiction.setIsRevision("1");

        return jurisdictionMapper.insertSelective(jurisdiction);
    }

    /**
     * 权限id集查询所有
     * @param list 权限id集
     * @return 权限模型集
     */
    @Override
    public List<OaJurisdiction> selectAll(List<Long> list) {
        return jurisdictionMapper.selectAll(list);
    }

    /**
     * 批量删除
     * @param list 权限id集
     * @return 回调函数
     */
    @Override
    public int deleteAll(List<Long> list) {
        return jurisdictionMapper.deleteAll(list);
    }

    /**
     * id删除
     * @param id 权限id
     * @return 回调函数
     */
    @Override
    public int deleteById(Long id) {
        return jurisdictionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询一级标题
     * @param id 权限id 用于排除当前权限信息
     * @return 权限模型集
     */
    @Override
    public List<OaJurisdiction> selectAllOne(Long id) {
        return jurisdictionMapper.selectAllOne(id);
    }

    @Override
    public List<ThreeVo> findByIds(Long role) {

          //查出来所有的权限
          List<OaJurisdiction> oaJurisdictions = jurisdictionMapper.selectAll(null);
            //根据角色Id查询它所拥有的权限
            List<Long> byIds = jurisdictionRoleMapper.findByIds(role);
            List<ThreeVo> trees=new ArrayList<>();
        for (OaJurisdiction oaJurisdiction : oaJurisdictions) {
            ThreeVo threeVo=new ThreeVo();
            for (Long byId : byIds) {
                if(oaJurisdiction.getPkJurisdictionId().equals(byId) && (!oaJurisdiction.getJurisdictionSuperior().equals(0L)||"0".equals(oaJurisdiction.getJurisdictionType()))){
                    threeVo.setSelected(true);
                }
            }
            // 菜单名称
            threeVo.setName(oaJurisdiction.getJurisdictionName());
            // 菜单id
            threeVo.setValue(oaJurisdiction.getPkJurisdictionId());
            if(oaJurisdiction.getJurisdictionSuperior()==0){
                threeVo.setSuperId(0L);
            }else{
                threeVo.setSuperId(oaJurisdiction.getJurisdictionSuperior());
            }
            trees.add(threeVo);
        }
        return tools.createTree(trees);
    }

    @Override
    public Integer isExist(String keyword) {
        return jurisdictionMapper.isExist(keyword);
    }
}
